package data_views

import (
	"github.com/labstack/echo/v4"
	"rabbit_love/src/service/client/daos/data_daos"
	"rabbit_love/src/service/client/forms/request/data_forms"
	"rabbit_love/src/service/client/utils"
	"rabbit_love/src/utils/response"
)

func GetMarkListApi(ctx echo.Context) error {
	var (
		loverId int
	)

	loverId = utils.GetLoverId(ctx)
	if loverId == 0 {
		return response.RespErr(ctx, "用户验证失败，请重新登录")
	}

	var (
		data []map[string]interface{}
		err  error
	)

	data, err = data_daos.GetMarkListDao(loverId)
	if err != nil {
		return response.RespErr(ctx, "获取失败")
	}
	return response.RespSuccess(ctx, "操作成功", data)
}

func AddMarkListApi(ctx echo.Context) error {
	var (
		form data_forms.AddMarkRequest
		err  error
	)

	if err = ctx.Bind(&form); err != nil {
		return response.RespErr(ctx, "数据格式错误")
	}

	if err = form.Validator(); err != nil {
		return response.RespErr(ctx, "请检查数据是否填写完整")
	}

	var (
		userId  int
		loverId int
	)
	userId = utils.GetUserId(ctx)
	loverId = utils.GetLoverId(ctx)
	if userId == 0 || loverId == 0 {
		return response.RespErr(ctx, "用户验证失败，请重新登录")
	}

	if err = data_daos.AddMarkListDao(userId, loverId, &form); err != nil {
		return response.RespErr(ctx, err.Error())
	}
	return response.RespSuccess(ctx, "操作成功", "操作成功")
}

func UpdMarkListApi(ctx echo.Context) error {
	var (
		form data_forms.UpdMarkRequest
		err  error
	)

	if err = ctx.Bind(&form); err != nil {
		return response.RespErr(ctx, "数据格式错误")
	}

	if err = form.Validator(); err != nil {
		return response.RespErr(ctx, "请检查数据是否填写完整")
	}

	var (
		userId  int
		loverId int
	)
	userId = utils.GetUserId(ctx)
	loverId = utils.GetLoverId(ctx)
	if userId == 0 || loverId == 0 {
		return response.RespErr(ctx, "用户验证失败，请重新登录")
	}

	if err = data_daos.UpdMarkListDao(userId, loverId, &form); err != nil {
		return response.RespErr(ctx, err.Error())
	}
	return response.RespSuccess(ctx, "操作成功", "操作成功")
}

func DelMarkListApi(ctx echo.Context) error {
	var (
		form data_forms.DelMarkRequest
		err  error
	)

	if err = ctx.Bind(&form); err != nil {
		return response.RespErr(ctx, "数据格式错误")
	}

	if err = form.Validator(); err != nil {
		return response.RespErr(ctx, "请检查数据是否填写完整")
	}

	var (
		userId  int
		loverId int
	)
	userId = utils.GetUserId(ctx)
	loverId = utils.GetLoverId(ctx)
	if userId == 0 || loverId == 0 {
		return response.RespErr(ctx, "用户验证失败，请重新登录")
	}

	if err = data_daos.DelMarkListDao(userId, loverId, int(form.MarkId)); err != nil {
		return response.RespErr(ctx, err.Error())
	}
	return response.RespSuccess(ctx, "操作成功", "操作成功")
}
